﻿using Common.Core.IoC;

namespace Common.Core.Cache
{
    /// <summary>
    /// 缓存操作帮助类
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        /// 缓存接口
        /// </summary>
        private ICacheService cacheservice;

        /// <summary>
        /// 使用MemoryCache缓存操作
        /// </summary>
        /// <param name="isMemoryCache">是否使用MemoryCache</param>
        public CacheHelper(bool isMemoryCache = false)
        {

            if (isMemoryCache)
            {
                cacheservice = IoCContainer.Resolve<MemoryCacheService>();
            }
            else
            {
                cacheservice = IoCContainer.Resolve<ICacheService>();

            }
        }

        #region 验证缓存项是否存在
        /// <summary>
        /// 验证缓存项是否存在,TryGetValue 来检测 Key是否存在的
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public bool Exists(string key)
        {
            return cacheservice.Exists(key);
        }
        #endregion

        #region 添加缓存

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <returns></returns>
        public bool Add(string key, object value)
        {
            return cacheservice.Add(key, value);
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="expiresSliding">滑动过期时长（如果在过期时间内有操作，则以当前时间点延长过期时间,Redis中无效）</param>
        /// <param name="expiressAbsoulte">绝对过期时长</param>
        /// <returns></returns>
        public bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
        {
            return cacheservice.Add(key, value, expiresSliding, expiressAbsoulte);
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="expiresIn">缓存时长</param>
        /// <param name="isSliding">是否滑动过期（如果在过期时间内有操作，则以当前时间点延长过期时间,Redis中无效）</param>
        /// <returns></returns>
        public bool Add(string key, object value, TimeSpan expiresIn, bool isSliding = false)
        {
            return cacheservice.Add(key,value,expiresIn,isSliding);
        }
        #endregion

        #region 删除缓存

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            return cacheservice.Remove(key);
        }
        /// <summary>
        /// 批量删除缓存
        /// </summary>
        /// <param name="keys">缓存Key集合</param>
        /// <returns></returns>
        public void RemoveAll(IEnumerable<string> keys)
        {
            cacheservice.RemoveAll(keys);
        }
        /// <summary>
        /// 使用通配符找出所有的key然后逐个删除
        /// </summary>
        /// <param name="pattern">通配符，前后会自动增加*号，比如Admin:UserInfo:Id</param>
        /// <returns></returns>
        public void RemoveByPattern(string pattern)
        {
            cacheservice.RemoveByPattern(pattern);
        }
        /// <summary>
        /// 删除所有缓存
        /// </summary>
        public void RemoveCacheAll()
        {
            cacheservice.RemoveCacheAll();
        }
        /// <summary>
        /// 删除模糊匹配缓存【仅支持Redis】，通过命令删除缓存，主要是用于前缀一样的情况。
        /// </summary>
        /// <param name="key">模糊key值，需要自己写通配符*号，比如：Admin:UserInfo:Id:*</param>
        /// <returns></returns>
        public bool RemoveCacheByKey(string key)
        {
            return cacheservice.RemoveCacheByKey(key);
        }
        #endregion

        #region 获取缓存
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public T Get<T>(string key) where T : class
        {
            return cacheservice.Get<T>(key);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public object Get(string key)
        {
            return cacheservice.Get(key);
        }
        /// <summary>
        /// 获取缓存集合
        /// </summary>
        /// <param name="keys">缓存Key集合</param>
        /// <returns></returns>
        public IDictionary<string, object> GetAll(IEnumerable<string> keys)
        {
            return cacheservice.GetAll(keys);
        }

        #endregion

        #region 修改缓存
        /// <summary>
        /// 修改缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">新的缓存Value</param>
        /// <returns></returns>
        public bool Replace(string key, object value)
        {
            return cacheservice.Replace(key, value);

        }
        /// <summary>
        /// 修改缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">新的缓存Value</param>
        /// <param name="expiresSliding">滑动过期时长（如果在过期时间内有操作，则以当前时间点延长过期时间）</param>
        /// <param name="expiressAbsoulte">绝对过期时长</param>
        /// <returns></returns>
        public bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
        {
            return cacheservice.Replace(key, value, expiresSliding, expiressAbsoulte);
        }
        /// <summary>
        /// 修改缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">新的缓存Value</param>
        /// <param name="expiresIn">缓存时长</param>
        /// <param name="isSliding">是否滑动过期（如果在过期时间内有操作，则以当前时间点延长过期时间）</param>
        /// <returns></returns>
        public bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false)
        {
            return cacheservice.Replace(key,value,expiresIn,isSliding);
        }
        #endregion

        #region 其他

        #region 步长1的递增、递减
        /// <summary>
        /// 为数字增长val
        /// </summary>
        /// <param name="key">缓存键值</param>
        /// <param name="value">缓存值</param>
        /// <param name="cacheTime">过期时间</param>
        public double StringIncrement(string key, double value, TimeSpan? cacheTime)
        {
            return cacheservice.StringIncrement(key, value, cacheTime);
        }

        /// <summary>
        /// 为数字减少val
        /// </summary>
        /// <param name="key">缓存键值</param>
        /// <param name="value">缓存值</param>
        /// <param name="cacheTime">过期时间</param>
        public double StringDecrement(string key, double value, TimeSpan? cacheTime)
        {
            return cacheservice.StringDecrement(key, value, cacheTime);
        }

        #endregion

        #region 分布式锁

        /// <summary>
        /// redis加锁操作
        /// </summary>
        /// <param name="key">锁的名称，不可重复</param>
        /// <param name="expireTimeSeconds">锁过期时间</param>
        public void Lock(string key, int expireTimeSeconds = 10)
        {
            cacheservice.Lock(key, expireTimeSeconds);
        }

        /// <summary>
        /// redis解锁操作
        /// </summary>
        /// <param name="key">锁的名称</param>
        /// <returns></returns>
        public bool UnLock(string key)
        {
            return cacheservice.UnLock(key);
        }

        #endregion

        #endregion

    }
}
